package com.pactera.remote;

import com.pactera.config.RestTemplateConfig;
import com.pactera.constant.Constant;
import com.pactera.util.DateUtil;
import com.pactera.util.JsonUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by 17894 on 2021/3/10.
 */

@Service
public class RemoteService extends AbstractRemoteService {

    @Autowired
    private RestTemplateConfig restTemplateConfig;

    private static Logger logger = LoggerFactory.getLogger(RemoteService.class);








    public Map<String, Object> getCityInfoByCityName(String cityName) throws Exception {
        String result = null;
        try {
//            result = doGet(Constant.CITY_REQUEST_URL, cityName);
            result = restTemplateConfig.restTemplate().getForObject(Constant.CITY_REQUEST_URL + Constant.LOCATION_PENDING_STR + cityName, String.class);
            Map<String, Object> map = JsonUtil.parseJsonStringToMap(result);
            logger.info("返回城市信息{}",JsonUtil.objectToJson(map));
            return map;
        } catch (Exception e) {
            logger.error("通过城市名称【{}】获取城市信息异常", cityName, e);
        }
        return null;
    }


    public Map<String, Object> getCityWeatherInfoByCityId(String cityId) throws Exception {
        String result = null;
        try {
//            result = doGet(Constant.CITY_WEATHER_REQUEST_URL, cityId);
            result = restTemplateConfig.restTemplate().getForObject(Constant.CITY_WEATHER_REQUEST_URL + Constant.LOCATION_PENDING_STR + cityId, String.class);
            Map<String, Object> map = JsonUtil.parseJsonStringToMap(result);
            String updateTime = Objects.isNull(map.get("updateTime")) ? "" : map.get("updateTime").toString().replace("T"," ");
            map.put("updateTime", DateUtil.format(DateUtil.parse(StringUtils.left(updateTime,16),"yyyy-MM-dd HH:mm"),"E H a"));
            logger.info("返回城市天气信息{}",JsonUtil.objectToJson(map));
            return map;
        } catch (Exception e) {
            logger.error("通过城市标识【{}】获取城市天气信息异常", cityId, e);
        }
        return null;
    }


    public Map<String, List<String>> get() {
        String jsonStr = null;
        try {
            jsonStr = restTemplateConfig.restTemplate().getForObject(Constant.PETS_URL, String.class);
            List<Map> list = JsonUtil.jsonToList(jsonStr,Map.class);
            Map<String,List<String>> resultMap = new HashMap<>();
            list.stream().forEach(p ->{
                String gender = Objects.isNull(p.get(Constant.GENDER)) ? Constant.EMPTY_STR :p.get(Constant.GENDER).toString();
                String name = Objects.isNull(p.get(Constant.NAME)) ? Constant.EMPTY_STR :p.get(Constant.NAME).toString();
                if (!resultMap.containsKey(gender)) {
                    List<String> userList = new ArrayList<>();
                    userList.add(name);
                    resultMap.put(gender, userList);
                } else {
                    resultMap.get(gender).add(name);
                }
            });
            for(List tempList:resultMap.values()) {
                Collections.sort(tempList);
            }
            logger.info("返回宠物列表JSON信息{}", JsonUtil.objectToJson(resultMap));
            return resultMap;
        } catch (Exception e) {
            logger.error("加载宠物信息异常", e);
        }
        return null;
    }
}
